package cn.school.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringKit {
	private StringKit() {
	}

	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	public static Date toDate(String str) {
		try {
			if (str != null && str != "")
				return sdf.parse(str);
		} catch (ParseException e) {
			// e.printStackTrace();
		}
		return null;
	}

	public static int toInt(String str, int defaultValue) {
		try {
			return Integer.parseInt(str);
		} catch (NumberFormatException e) {
			// e.printStackTrace();
		}
		return defaultValue;
	}

	public static int toInt(String str) {
		try {
			return Integer.parseInt(str);
		} catch (NumberFormatException e) {
			// e.printStackTrace();
		}
		return 0;
	}

	public static long toLong(String str) {
		try {
			return Long.parseLong(str);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return 0L;
	}

	public static String dateToString(Date date) {
		try {
			if (date != null)
				return sdf.format(date);
		} catch (Exception e) {
			// e.printStackTrace();
		}
		return null;
	}

	public static String converNull(String str) {
		if (str == null) {
			str = "";
		}
		return str;
	}
}
